package jd

import (
	"bufio"
	"fmt"
	"io"
	"os"
	//"reflect"
)

func GetFileStart() {
	provineMap := GetFile("provine20151016152552.txt")
	fmt.Println("provineMap=", len(provineMap), provineMap)

	cityMap := GetFile("city20151016152552.txt")
	fmt.Println("cityMap=", len(cityMap), cityMap)

	countysMap := GetFile("countys20151016152552.txt")
	fmt.Println("countysMap=", len(countysMap), countysMap)
	provine := provineMap["root"]
	var p, c, y = 0, 0, 0
	for pcode, pname := range provine {
		fmt.Println(pcode, pname)
		city := cityMap[pcode]
		for ccode, cname := range city {
			fmt.Println("\t", ccode, cname)
			countys := countysMap[ccode]
			for ycode, yname := range countys {
				fmt.Println("\t\t", ycode, yname)
				y++
			}
			c++
		}
		p++
	}
	fmt.Println("共", p, "个省,", c, "个省区", y, "个地区")
}
func GetFile(fileName string) map[string]map[string]string {
	file, _ := os.Open(filePath + fileName)
	defer file.Close()
	rd := bufio.NewReader(file)
	resMap := make(map[string]map[string]string)
	for {
		line, err := rd.ReadString('\n') //以'\n'为结束符读入一行
		if err != nil || io.EOF == err {
			break
		}
		mmap := ToMap(line)
		key := ""
		stat := mmap["stat"].(map[string]interface{})
		data := mmap["data"].([]interface{})
		key = stat["pcode"].(string)
		mp1 := make(map[string]string)
		for _, v := range data {
			mp := v.(map[string]interface{})
			mp1[mp["code"].(string)] = mp["name"].(string)
		}
		resMap[key] = mp1
	}
	return resMap
}
